import {
  BasicLayout,
  BlankLayout,
  RouteView,
  PageView,
  Iframe,
} from "@/layouts/index.js";

// 前端未找到页面路由（固定不用改）、原来为 /404
const notFoundRouter = {
  path: "*",
  redirect: "/",
  hidden: true,
};

// 前端路由表(页面位置component)
const constantRouterComponents = {
  BasicLayout: BasicLayout,
  BlankLayout: BlankLayout,
  RouteView: RouteView,
  PageView: PageView,
  Iframe: Iframe,
  404: () => import("@/views/userLoginReg/Error"),

  // 用户信息维护页面
  UserProfile: () => import("@/views/system/account/settings/UserProfile"),
};

// 个人中心页面
const userAccount = [
  // account
  {
    name: "个人中心",
    path: "/account/user-profile",
    pid: 0,
    id: 10028,
    meta: {
      title: "用户信息",
      icon: "user",
      show: false,
    },
    component: "UserProfile",
  },
];

// 根级菜单
const rootRouter = {
  key: "",
  name: "MenuIndex",
  path: "",
  component: "BasicLayout",
  redirect: "/dashboard",
  meta: { title: "首页" },
  children: [],
};

/**
 * 动态生成菜单
 * @param data
 * @returns {Promise<Router>}
 */
export const generatorDynamicRouter = (data) => {
  return new Promise((resolve, reject) => {
    const resNav = data.menus;
    const menuNav = [];
    const childrenNav = [];
    // 后端数据, 根级树数组,  根级 PID
    // 这里设置根目录
    listToTree(resNav, childrenNav, 0);

    //增加静态网页
    listToTree(userAccount, childrenNav, 0);
    rootRouter.children = childrenNav;
    menuNav.push(rootRouter);

    const routers = generator(menuNav);
    routers.push(notFoundRouter);
    resolve(routers);
  }).catch((err) => {
    // reject('加载菜单失败')
    return Promise.reject(err);
  });
};

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generator = (routerMap, parent) => {
  return routerMap.map((item) => {
    // eslint-disable-next-line no-unused-vars
    const {
      title,
      show,
      hideChildren,
      hiddenHeaderContent,
      target,
      icon,
      link,
    } = item.meta || {};
    const currentRouter = {
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: item.path || `${(parent && parent.path) || ""}/${item.key}`,
      // 路由名称，建议唯一
      name: item.name || item.key || "",
      // 该路由对应页面的 组件 :方案1
      // component: constantRouterComponents[item.component || item.key],
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component:
        constantRouterComponents[item.component || item.key] ||
        (() => import(`@/views/${item.component}`)),
      // meta: 页面标题, 菜单图标, 页面权限(供指令权限用，可去掉)
      meta: {
        title: title,
        icon: icon || undefined,
        // hiddenHeaderContent: hiddenHeaderContent,
        target: target,
        link: link,
      },
    };
    // 是否设置了隐藏菜单
    if (show === false) {
      currentRouter.hidden = true;
    }
    // 是否设置了隐藏子菜单
    if (hideChildren) {
      currentRouter.hideChildrenInMenu = true;
    }
    // 为了防止出现后端返回结果不规范，处理有可能出现拼接出两个 反斜杠
    if (!currentRouter.path.startsWith("http")) {
      currentRouter.path = currentRouter.path.replace("//", "/");
    }
    // 重定向
    item.redirect && (currentRouter.redirect = item.redirect);
    // 是否有子菜单，并递归处理
    if (item.children && item.children.length > 0) {
      // Recursion
      currentRouter.children = generator(item.children, currentRouter);
    }
    return currentRouter;
  });
};

/**
 * 数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
const listToTree = (list, tree, parentId) => {
  if (list) {
    list.forEach((item) => {
      // 判断是否为父级菜单
      // eslint-disable-next-line eqeqeq
      if (item.pid == parentId) {
        const child = {
          ...item,
          key: item.key || item.name,
          children: [],
        };
        // 迭代 list， 找到当前菜单相符合的所有子菜单
        listToTree(list, child.children, item.id);
        // 删掉不存在 children 值的属性
        if (child.children.length <= 0) {
          delete child.children;
        }
        // 加入到树中
        tree.push(child);
      }
    });
  }
};
